﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuteWebUI;

public partial class Admin_Video : System.Web.UI.Page
{

    protected LocalFunctions FUNC;
    Int32 iGalleryID;
    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Admin_AdminMasterPage)Master).FUNC;
        
        GetParameters();

        Dal.arParams.Clear();
        Dal.arParams.Add(1);
        Dal.arParams.Add(iGalleryID);
        DataSet DS = Dal.ExecuteDBspDS("RD_GetGalleryByID", Dal.arParams);
        if (DS != null)
        {
            // Set gallery title
            if (DS.Tables.Count > 0)
            {
                lblTitle.Text = DS.Tables[0].Rows[0]["Title"].ToString();
            }
        }

        if (!IsPostBack)
            GetVideoData();
    }

    private void GetVideoData()
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(iGalleryID);
        DataTable DT = Dal.ExecuteDBsp("Admin_GetVideoByGalID", Dal.arParams);
        gvVideo.DataSource = DT;
        gvVideo.DataBind();
    }

    private void GetParameters()
    {
        iGalleryID= Convert.ToInt32(Request.QueryString["Id"]);
    }

    protected void gvVideo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvVideo.EditIndex = e.NewEditIndex;

        GetVideoData();
    }

    protected void gvVideo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvVideo.EditIndex = -1;
        GetVideoData();
    }

    protected void gvVideo_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = gvVideo.Rows[e.RowIndex];
        string sDescRu = ((TextBox)row.FindControl("txtDescRu")).Text;
        string sDescHeb = ((TextBox)row.FindControl("txtDescHeb")).Text;
        string sLink = ((TextBox)row.FindControl("txtLink")).Text;

        Int32 iID = Convert.ToInt32(((Label)row.FindControl("lblID")).Text);
        
        Dal.arParams.Clear();
        Dal.arParams.Add(iID);
        Dal.arParams.Add(sLink);
        Dal.arParams.Add(sDescRu);
        Dal.arParams.Add(sDescHeb);
        
        Dal.ExecuteDBsp("Admin_UpdateVideo", Dal.arParams,true);

        gvVideo.EditIndex = -1;
        GetVideoData();
    }

    protected void gvVideo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = gvVideo.Rows[e.RowIndex];

        Int32 iID = Convert.ToInt32(((Label)row.FindControl("lblID")).Text);

        Dal.arParams.Clear();
        Dal.arParams.Add(iID);

        Dal.ExecuteDBsp("Admin_DeleteVideo", Dal.arParams);

        GetVideoData();
    }

    protected void lbAddNew_Click(object sender, EventArgs e)
    {
        GridViewRow row = gvVideo.FooterRow;
        String sDescRu = string.Empty;
        String sDescHeb = string.Empty;
        String sLink = string.Empty;
        String sFileName = string.Empty;

        if (row != null)
        {
            sDescRu = ((TextBox)row.FindControl("txtDescRu")).Text;
            sDescHeb = ((TextBox)row.FindControl("txtDescHeb")).Text;
            sLink = ((TextBox)row.FindControl("txtLink")).Text;

        }
        else
        {
            GridViewRow gvr = (GridViewRow)gvVideo.Controls[0].Controls[0];
            TableCell Cell = gvr.Cells[0];
            sDescRu = ((TextBox)Cell.FindControl("txtDescRu")).Text;
            sDescHeb = ((TextBox)Cell.FindControl("txtDescHeb")).Text;
            sLink = ((TextBox)Cell.FindControl("txtLink")).Text;
        }

        if (sLink != string.Empty && sDescRu != string.Empty && sDescHeb != string.Empty)
        Dal.arParams.Clear();
        Dal.arParams.Add(iGalleryID);
        Dal.arParams.Add(sLink);
        Dal.arParams.Add(sDescRu);
        Dal.arParams.Add(sDescHeb);
        Dal.ExecuteDBsp("Admin_AddVideoLink", Dal.arParams, true);
        
        GetVideoData();
    }
}
